package a51qindian.com.comprehensive.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.li.widget.DoubleTextView;

import java.util.List;

import a51qindian.com.comprehensive.R;
import a51qindian.com.comprehensive.model.CityDeviceInfo;
import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Author: Li
 * Date: 2017/10/16 0016
 * Description: 城市设备管理
 */

public class CityDeviceAdapter extends RecyclerView.Adapter<CityDeviceAdapter.CityDeviceHolder> {


    private LayoutInflater mInflater;

    public void setDeviceInfoList(List<CityDeviceInfo> deviceInfoList) {
        mDeviceInfoList = deviceInfoList;
        notifyDataSetChanged();
    }

    private List<CityDeviceInfo> mDeviceInfoList;

    public CityDeviceAdapter(Context context) {
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public CityDeviceHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_city_device, parent, false);
        return new CityDeviceHolder(view);
    }

    @Override
    public void onBindViewHolder(CityDeviceHolder holder, int position) {
        CityDeviceInfo deviceInfo = mDeviceInfoList.get(position);
        holder.mTxtNum.setTxtRight(deviceInfo.getSn());
        holder.mTxtAddress.setTxtRight(deviceInfo.getAddress());
        holder.mTxtContact.setTxtRight(deviceInfo.getName());
        holder.mTxtMobile.setTxtRight(deviceInfo.getMobile());
        holder.mTxtTitle.setText(deviceInfo.getShopName());
        if (isFirst(position)) {
            holder.mTxtTitle.setVisibility(View.GONE);
        }else {
            holder.mTxtTitle.setVisibility(View.GONE);
        }
    }


    private boolean isFirst(int pos){
        return pos==0||!mDeviceInfoList.get(pos).getShopName().equals(mDeviceInfoList.get(pos-1).getShopName());
    }
    @Override
    public int getItemCount() {
        return mDeviceInfoList == null ? 0 : mDeviceInfoList.size();
    }

    static class CityDeviceHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.txt_title)
        TextView mTxtTitle;
        @BindView(R.id.txt_num)
        DoubleTextView mTxtNum;
        @BindView(R.id.txt_contact)
        DoubleTextView mTxtContact;
        @BindView(R.id.txt_mobile)
        DoubleTextView mTxtMobile;
        @BindView(R.id.txt_address)
        DoubleTextView mTxtAddress;
        public CityDeviceHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

}
